home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9994 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c,comp.lang.modula3,comp.lang.modula2
  4. Subject: Re: Hungarian notation - whoops!
  5. Date: 5 Mar 1996 07:23:23 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4hhm9bINNqa3@keats.ugrad.cs.ubc.ca>
  8. References: <30C40F77.53B5@swsbbs.com> <4h6hlo$hqu@goanna.cs.rmit.EDU.AU> <4h7vgdINNmsh@anvil.ugrad.cs.ubc.ca> <4hgd11$4p4@goanna.cs.rmit.EDU.AU>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hgd11$4p4@goanna.cs.rmit.EDU.AU>,
  12. Richard A. O'Keefe <ok@goanna.cs.rmit.EDU.AU> wrote:
  13. >In all this disucssion I have not once stated or implied that >>C<< is bad.
  14. >I have been complaining about TWOS COMPLEMENT ARITHMETIC, which is in no
  15. >way synonymous with C.
  16. >
  17. >For another example of the peculiarities of twos complement arithmetic,
  18. >consider the fact that an arithmetic right shift on a ones complement or
  19. >sign and magnitude machine is always equivalent to division by a power
  20. >of 2, but not in a twos complement machine.  (I note that the C standard
  21. >does not guarantee that there will be an arithmetic right shift, so please
  22. >don't mistake this for a C criticism.)
  23.  
  24. Okay, I think I have your point of view disilled into a clear form now after
  25. back and forth revisions and clarifications.
  26.  
  27. So what are we to do? It is true that: some operations cannot be expressed in
  28. the ``obvious way'' on two's complement arithmetic. However, it is also true
  29. that some operations that are expressed in the ``obvious way'' under two's
  30. complement _fail_ under a sign and magnitude machines!
  31.  
  32. The road goes both ways. Shall I give an example?
  33.  
  34. Under two's complement, I can convert a signed quantity into an unsigned
  35. quantity such that the unsigned is _congruent_ to the signed, modulo a power of
  36. two. I can do this just by chopping bits (if the unsigned quantity is as
  37. narrow, or narrower than the signed type being converted). This is how standard
  38. C defines the operation of converting a signed integral type into an unsigned
  39. quantity that is narrower (K&R6.1, I think).
  40.  
  41. You can't do this under sign and magnitude representation. Elsewhere in this
  42. newsgroup (comp.lang.c) someone criticized me for not using a cast from integer
  43. to unsigned short to obtain the lower CHAR_BITS of the integer value. I replied
  44. that this obvious way only works in two's complement, since the cast is defined
  45. as finding the congruence, which is only equivalent to bit chopping under two's
  46. complement. I have some code that _depends_ on this behavior, and is restricted
  47. to machines that have two's complement arithmetic and eight bit bytes/32 bit
  48. longs. But then again, the code emulates a microprocessor that uses two's
  49. complement arithmetic, so it's a biased example! :)
  50.  
  51. Don't tell me that two's complement doesn't have its neat and useful aspects!
  52. It's a great idea that has been used successfully in many microprocessors! Of
  53. course, those microprocessors also give you overflow checks when you program
  54. them at the low level, that much is true.
  55. -- 
  56.  
  57.